<template>
    <div id="companyRegister" class="login_bg">
        <common-login :commonData="{$parent:$parent}"></common-login>
        <div class="login_box company_register_box company_register">
            <ul class="text_center login_box_ul register_box_ul">
                <li>
                    <router-link to="/register">个人注册</router-link>
                </li>
                <li>
                    <router-link class="login_tab_active" to="/company_register">企业注册</router-link>
                </li>
            </ul>
            <form ref="form" :model="form" @keyup.enter="toCompanyRegister(form)">
                <div class="company_register_left">
                    <div class="login_input_box">
                        <img class="login_icon" src="../../static/img/login/login_company.png">
                        <input type="text" v-model.trim="form.companyName" placeholder="请输入您的企业名称">
                        <a v-if="form.companyName.length>3" class="qichacha" target="_blank" :href="'https://aiqicha.baidu.com/s?q='+form.companyName">爱企查</a>
                    </div>
                    <div class="login_input_box">
                        <img class="login_icon" src="../../static/img/login/login_xinyong.png">
                        <input type="text" v-model.trim="form.identification" placeholder="请输入社会信用代码">
                    </div>
                    <div class="login_input_box">
                        <img class="login_icon" src="../../static/img/login/login_user.png">
                        <input type="text" v-model.trim="form.telephone" placeholder="请输入绑定手机号">
                    </div>
                    <div class="login_input_box login_input_code">
                        <img class="login_icon" src="../../static/img/login/login_code.png">
                        <input style="width: 36%;" type="text" v-model.trim="form.code" placeholder="请输入验证码">
                        <input class="register_getcode" readonly="readonly" :value="verifyCode" :disabled="disabled"
                            @click="getVerifyCode" />
                    </div>
                    <div class="login_input_box">
                        <img class="login_icon" src="../../static/img/login/login_pwd.png">
                        <input type="password" v-model.trim="form.pwd" placeholder="请输入您的密码">
                    </div>
                    <div class="login_input_box">
                        <img class="login_icon" src="../../static/img/login/login_rePwd.png">
                        <input type="password" v-model.trim="form.rePwd" placeholder="请再次确认您的密码">
                    </div>
                </div>
                <div class="company_register_right text_center">
                    <p class="relative">请上传营业执照：</p>
                    <div class="company_register_right_box relative">
                        <el-upload drag class="upload-demo" list-type="picture" :headers="headers" :action="uploadUrl()"
                            :before-upload="onBeforeUpload" :show-file-list="false" :on-success="handleAvatarSuccess">
                            <img v-if="form.base64" :src="'data:image/png;base64,'+form.base64" class="avatar" width="100%">
                            <img class="login_icon_upload" v-else src="/static/img/login/upload_icon.png">
                        </el-upload>
                    </div>
                </div>
                <div class="clear"></div>
                <p class="registerAttentionP">注：以上信息用于考试报名，请谨慎填写</p>
                <div class="company_register_btns text_center">
                    <a class="login_btn" @click="toCompanyRegister(form)">立即注册</a>
                    <router-link class="login_a" to="/company_forget">忘记密码</router-link>
                    <span>|</span>
                    <router-link class="login_a" to="/company_login">点击登录</router-link>
                </div>
            </form>
        </div>
    </div>
</template>

<script>
    import commonLogin from '../components/c_login.vue'
    export default {
        name: 'company_forget',
        data() {
            return {
                // hrefs:"https://www.qichacha.com/search?key="+this.form.companyName,
                verifyCode: "获取短信验证码",
                disabled: false,
                areas: [],
                form: {
                    companyName: "",
                    identification: "",
                    telephone: "",
                    v_token:"",
                    code: "",
                    pwd: "",
                    rePwd: "",
                    base64: ""
                }
            }
        },
        created: function() {
        },
        computed:{
            headers(){
                return{
                    version:this.api.version
                }
            }
        },
        mounted: function() {},
        methods: {
            onBeforeUpload: function(file) {
                var isIMAGE = ((file.type === 'image/jpeg') || (file.type === 'image/png') || (file.type ===
                    'image/bmp') || (file.type === 'image/jpg'));
                if (!isIMAGE) {
                    this.$message.error('上传文件只能是图片格式（jpeg/png/bmp/jpg）');
                    return false
                }
                if (file.size / 1024 > 200) {
                    // console.log(file.size)
                    // this.$message.error('上传图片不能大于30kb');
                    this.$alert('图片不能大于200kb', '异常提示', {
                        confirmButtonText: '确定',
                        type: 'warning'
                    });
                    return false
                }
            },
            uploadUrl: function() {
                return this.api.base64PngUpload;
            },
            handleAvatarSuccess(res, file) {
                this.form.base64 =  res.resultMap;
            },
            getVerifyCode: function() {
                var that = this;
                var telephone = this.form.telephone;
                if (telephone == "") {
                    this.$message.error('请输入手机号！');
                    return;
                }
                if (telephone.length != 11) {
                    this.$message.error('请输入正确的手机号！');
                    return;
                }
                this.common.post(this.api.getVerifyCodeRegister, {
                    telephone: telephone
                }, function(data) {
					that.form.v_token=data.resultMap;
                    that.setInter();
                }, function(error) {
                    that.$message.error(error.resultMsg);
                });
            },
            setInter: function() {
                var that = this;
                var countDown = 60;
                var timer = setInterval(function() {
                    countDown--;
                    that.verifyCode = "短信已发送（" + countDown + "）";
                    that.disabled = true;
                    if (countDown < 1) {
                        clearInterval(timer);
                        that.verifyCode = "获取短信验证码";
                        that.disabled = false;
                    }
                }, 1000);
            },
            toCompanyRegister: function(dataForm) {
                // console.log(JSON.stringify(dataForm));
                var that = this;
                if (dataForm.companyName == "") {
                    this.$message.error('请输入企业名称！');
                    return;
                }
                if (dataForm.identification == "") {
                    this.$message.error('请输入社会信用代码！');
                    return;
                }
                if (dataForm.telephone == "") {
                    this.$message.error('请输入手机号！');
                    return;
                }
                if (dataForm.code == "") {
                    this.$message.error('请输入验证码！');
                    return;
                }
                if (dataForm.pwd == "") {
                    this.$message.error('请输入密码！');
                    return;
                }
                if (dataForm.rePwd == "") {
                    this.$message.error('请再次输入密码！');
                    return;
                }
                if (dataForm.pwd != dataForm.rePwd) {
                    this.$message.error('两次密码输入不一致！');
                    return;
                }
                if (dataForm.base64 == "") {
                    this.$message.error('请上传营业执照！');
                    return;
                }

                this.common.post(this.api.companyRegister, dataForm, function(data) {
                    that.common.toPage("/company_login");
                }, function(error) {
                    that.$message.error(error.resultMsg);
                });
            }
        },
        components: {
            "common-login": commonLogin
        }
    }
</script>
<style>

    .login_bg_register {
        height: auto !important;
    }

</style>
